//
//  MusicPlayViewController.m
//  XMLY
//
//  Created by lanou on 15/11/6.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "MusicPlayViewController.h"
#import "PlayView.h"
#import "PKPlayerManger.h"
@interface MusicPlayViewController ()
@property (nonatomic, strong)PlayView * playView;
@property (nonatomic, strong)NSMutableArray * musicUrlArr;
@property (nonatomic, strong)PKPlayerManger * player;
@property (nonatomic, strong)NSTimer * timer;
@end

@implementation MusicPlayViewController


-(NSMutableArray *)musicUrlArr
{
    if (_musicUrlArr==nil) {
        self.musicUrlArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _musicUrlArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _playView = [[PlayView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_playView];
    _playView.pictureView.image = [UIImage imageNamed:@"bofangbeijing"];
    [_playView configureViewWithModel:self.musicArr[self.index]];
    for (int i = 0; i<self.musicArr.count; i++) {
        NSString * playUrl = ((ChildListModel *)self.musicArr[i]).playUrl64;
        [self.musicUrlArr addObject:playUrl];
    }
    _player = [PKPlayerManger shareManger];
    [_playView.backBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [_player setMangerMusicArray:self.musicUrlArr];
    [_player playWithIndex:self.index];
    
    [_playView.beforeBtn addTarget:self action:@selector(didClickBeforeBtn) forControlEvents:(UIControlEventTouchUpInside)];
    [_playView.playAndPause addTarget:self action:@selector(didClickPlayAndPauseBtn) forControlEvents:(UIControlEventTouchUpInside)];
    [_playView.nextBtn addTarget:self action:@selector(didClickNextBtn) forControlEvents:(UIControlEventTouchUpInside)];
    [_playView.slider addTarget:self action:@selector(sliderBeginSlide:) forControlEvents:(UIControlEventValueChanged)];
    _timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
}

-(void)didClickNextBtn
{

    self.index++;
    if (self.index==self.musicUrlArr.count) {
        self.index=0;
    }
    [_playView configureViewWithModel:self.musicArr[self.index]];
    [_player playWithIndex:self.index];
    
    
}
-(void)didClickPlayAndPauseBtn
{

    
    
}

-(void)didClickBeforeBtn
{

   self.index--;
    if (self.index==-1) {
        self.index = self.musicUrlArr.count-1;
    }
    
    [_playView configureViewWithModel:self.musicArr[self.index]];
    [_player playWithIndex:self.index];
}

-(void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)timeChange{
       _playView.slider.minimumValue = 0.0;
    _playView.slider.maximumValue = _player.finishTime;
    
    NSUInteger yuTime = _player.finishTime - _player.currentTime;
    NSString *minutes = [NSString stringWithFormat:@"%02ld", yuTime / 60];
    NSString *seconds = [NSString stringWithFormat:@"%02ld", yuTime % 60];
    _playView.timeLabel.text = [[minutes stringByAppendingString:@":"] stringByAppendingString:seconds];
    _playView.slider.value = _player.currentTime;
    
    if (_player.currentTime !=0 && _player.finishTime-_player.currentTime==0) {
        [self didClickNextBtn];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
